package com.tecniplast.selene.listener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.ParseException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ServletContextAware;

import com.tecniplast.selene.common.util.WeiXinUtil;
import com.tecniplast.selene.factory.CacheFactory;
import com.tecniplast.selene.pojo.AccessToken;
import com.tecniplast.selene.pojo.AppInface;
import com.tecniplast.selene.pojo.AppInfo;
import com.tecniplast.selene.pojo.Syscode;
import com.tecniplast.selene.service.CommonService;

public class InitDataListener implements InitializingBean, ServletContextAware{  
	@Autowired
	private CommonService commonService;//属性。和配置文件对应property name对应  
    private Log log = LogFactory.getLog(this.getClass());
      
    @Override  
    public void afterPropertiesSet() throws Exception {  
        //在这个方法里面写 初始化的数据也可以。  
          
    }  
    @Override  
    public void setServletContext(ServletContext arg0) {  
        //userMap容器存放 userList（用户数据）  
        List<Syscode> syscodeList = new ArrayList<Syscode>();   
        List<AppInface> appinfaceList = new ArrayList<AppInface>();   
        try {  
        	//获取代码表
        	syscodeList = commonService.initSyscodeData();
            Map<String, Object> syscodeMap = CacheFactory.getInstance().createCache("syscodeList");  
            //一个工厂模式和单例模式的使用。文档最后有代码  
            for(Syscode s:syscodeList){  
            	syscodeMap.put(String.valueOf(s.getSysid()),s);  
            }                
            
            //获取微信常用接口
            appinfaceList = commonService.initAppInfData();
            Map<String, Object> appinfaceMap = CacheFactory.getInstance().createCache("appinface");  
            //一个工厂模式和单例模式的使用。文档最后有代码  
            for(AppInface a:appinfaceList){  
            	appinfaceMap.put(a.getType(),a);  
            }   
            initAppInfo(commonService);
        } catch (Exception e) {  
            log.error("query syscode error系统初始化查询所有用户出错!");  
            e.printStackTrace();
        }  
    }  
    
    public static void initAppInfo(CommonService commonService){
    	//获取账号信息
        AppInfo ai =  new AppInfo();
       if( CacheFactory.getInstance().isExist("appInfo")){
    	   Map<String,Object> map = CacheFactory.getInstance().getMapByKey("appInfo");
           ai = (AppInfo)map.get("appInfo");
       }else{
    	   List<AppInfo> aiList = commonService.initAppData();
    	   ai = aiList.get(0);
       }
       String appid = ai.getAppid();
       String appsecret = ai.getAppsecret();
       AccessToken token;
   		try {
   			token = WeiXinUtil.getAccessToken(appid, appsecret);
   			ai.setAccesstoken(token.getToken());
   	        Map<String, Object> appInfo = CacheFactory.getInstance().createCache("appInfo");
   	        appInfo.put("appInfo",ai );
   	        
   	        //保存token到数据库
   	        commonService.saveToken(token.getToken());
   		} catch (ParseException e) {
   			e.printStackTrace();
   		} catch (IOException e) {
   			e.printStackTrace();
   		}
    }
    
}
